<?php

namespace Biz\service\admin\tools;


use Illuminate\Routing\Router;

/**
 * laravel-admin 后台常用方法
*/
class SimpleRouteTpl
{

    private static $instance;
    private string $prefix = '';
    private string $controller = '';
    private string $module = '';
    private bool $hasPid = false;

    public function __construct($prefix, $controller, $hasPid = false)
    {
        $this->prefix = $prefix;
        $this->controller = $controller;
        $this->hasPid = $hasPid;
    }

    public static function getInstance($prefix, $controller, $hasPid = false)
    {
        if (empty(self::$instance[$prefix])) {
            self::$instance[$prefix] = new self($prefix, $controller, $hasPid);
        }
        return self::$instance[$prefix];
    }

    public static function getRoute($prefix, $module, $hasPid = false)
    {
        $key = $prefix . ":" . $module;
        if (empty(self::$instance[$key])) {
            self::$instance[$key] = new self($prefix, '', $hasPid);
            self::$instance[$key]->module = $module;
        }
        return self::$instance[$key];
    }

    public function getListRoute($pid = 0): string
    {
        return $this->prefix . "/{$this->module}" . ($pid ? "/{$pid}" : "") . "/index";
    }

    public function getCreateRoute($pid = 0, $params = []): string
    {
        return $this->getListRoute($pid) . "/create" . (empty($params) ? '' : '?' . http_build_query($params));
    }

    public function getEditRoute($id, $pid = 0): string
    {
        return $this->getListRoute($pid) . "/$id/edit";
    }

    public function getSaveRoute($pid = 0): string
    {
        return $this->getListRoute($pid) . "/save";
    }

    public function getExportRoute($pid = 0, $params = []): string
    {
        return $this->getListRoute($pid) . "/export" . (empty($params) ? '' : '?' . http_build_query($params));
    }

    public function routes(Router $router)
    {
        $controller = $this->controller;
        $base = $this->prefix . "/{module}";
        $uri = $base . "/index";
        $pBase = $this->prefix . "/{module}/{pid}";
        $pUri = $pBase . "/index";
        $router->get("$uri", "$controller@list");
        $router->get("$uri/create", "$controller@create");
        $router->get("$uri/export", "$controller@export");
        $router->get("$uri/{id}/edit", "$controller@create");
        $router->any("$uri/save", "$controller@save");
        $router->delete("$uri/{id}", "$controller@delete");
        $router->put("$uri/{id}", "$controller@put");
        $router->post("$base/sort", "$controller@sort");

        $router->get("$pUri", "$controller@pList");
        $router->get("$pUri/create", "$controller@pCreate");
        $router->get("$pUri/export", "$controller@pExport");
        $router->get("$pUri/{id}/edit", "$controller@pCreate");
        $router->any("$pUri/save", "$controller@pSave");
        $router->delete("$pUri/{id}", "$controller@pDelete");
        $router->put("$pUri/{id}", "$controller@pPut");
        $router->post("$pBase/sort", "$controller@pSort");
    }




}